package {{.PackageName}}.service.impl


import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import {{.PackageName}}.converter.{{.ClassName}}Converter;
import {{.PackageName}}.dto.{{.ClassName}}DTO;
import {{.PackageName}}.model.{{.ClassName}};
import {{.PackageName}}.repository.{{.ClassName}}Repository;
import {{.PackageName}}.service.{{.ClassName}}Service;

@Service
public class {{.ClassName}}ServiceImpl implements {{.ClassName}}Service {

    @Autowired
    private {{.ClassName}}Repository {{.LowerClassName}}Repository;


    @Autowired
    private {{.ClassName}}Converter {{.LowerClassName}}Converter;

    @Override
    public List<{{.ClassName}}DTO> findAll(Searchable searchable){
        final List<{{.ClassName}}> models = this.{{.LowerClassName}}Repository.findAll(searchable);
        return this.{{.LowerClassName}}Converter.toListDTO(models);
    }

    @Override
    public Page<{{.ClassName}}DTO> search(Pageable pageable, Searchable searchable){
        final Page<{{.ClassName}}> result = this.{{.LowerClassName}}Repository.search(searchable);
        return this.{{.LowerClassName}}Converter.toPageDTO(models);
    }

    @Override
    public {{.ClassName}}DTO create({{.ClassName}}DTO dto){
        final {{.ClassName}} model = this.{{.LowerClassName}}Converter.toModel(dto);
        // todo 增加实体的业务逻辑

        this.{{.LowerClassName}}Repository.save(model);
        return this.{{.LowerClassName}}Converter.toDTO(model);
    }

    @Override
    public {{.ClassName}}DTO update({{.ClassName}}DTO dto){
        final {{.ClassName}} model;
        try {
            model = this.{{.LowerClassName}}Repository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        // todo 增加实体的业务逻辑

        model = this.{{.LowerClassName}}Converter.toModel(dto);
        this.{{.LowerClassName}}Repository.save(model);

        return this.{{.LowerClassName}}Converter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.{{.LowerClassName}}Repository.deleteById(id);
    }

    @Override
    public {{.ClassName}}DTO details(Long id) {
        final {{.ClassName}} model;
        try {
            model = this.{{.LowerClassName}}Repository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.{{.LowerClassName}}Converter.toDTO(model);
    }


}